using System;
using System.Collections.Generic;
using System.Text;

using SD.LLBLGen.Pro.ORMSupportClasses;
using DatabaseAdapter;
using DatabaseGeneric;
using DatabaseGeneric.FactoryClasses;
using DatabaseGeneric.EntityClasses;
using DatabaseGeneric.RelationClasses;
using DatabaseGeneric.HelperClasses;

namespace Services
{
    public class SanPham : Interfaces.ISanPham
    {
        public EntityCollection<SanPhamEntity> FetchCollection()
        {
            EntityCollection<SanPhamEntity> collectionSanPham = new EntityCollection<SanPhamEntity>();
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(collectionSanPham, null);
            }
            return collectionSanPham;
        }

        public EntityCollection<SanPhamEntity> FetchCollection(ChungLoaiSanPhamEntity currentEntityChungLoaiSanPham)
        {
            EntityCollection<SanPhamEntity> collectionSanPham = new EntityCollection<SanPhamEntity>();
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntityCollection(collectionSanPham, new RelationPredicateBucket(SanPhamFields.MaChungLoaiSanPham == currentEntityChungLoaiSanPham.MaChungLoaiSanPham));
            }
            return collectionSanPham;
        }

        public EntityCollection<SanPhamEntity> FetchCollection(string criteria, int option)
        {
            EntityCollection<SanPhamEntity> collectionSanPham = new EntityCollection<SanPhamEntity>();
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                switch (option)
                {
                    case 1:
                        adapter.FetchEntityCollection(collectionSanPham, new RelationPredicateBucket(SanPhamFields.Phot == criteria));
                        break;

                    case 2:
                        adapter.FetchEntityCollection(collectionSanPham, new RelationPredicateBucket(SanPhamFields.RapMai == criteria));
                        break;

                    case 3:
                        adapter.FetchEntityCollection(collectionSanPham, new RelationPredicateBucket(SanPhamFields.BanhMai == criteria));
                        break;
                    case 4:
                        adapter.FetchEntityCollection(collectionSanPham, new RelationPredicateBucket(SanPhamFields.Last== criteria));
                        break;
                    case 5:
                        adapter.FetchEntityCollection(collectionSanPham, new RelationPredicateBucket(SanPhamFields.KhuonMat == criteria));
                        break;
                }
            }
            return collectionSanPham;
        }

        public SanPhamEntity FetchEntity(SanPhamEntity currentEntitySanPham)
        {
            SanPhamEntity entitySanPham = new SanPhamEntity(currentEntitySanPham.MaSanPham);
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.FetchEntity(entitySanPham);
            }
            return entitySanPham;
        }

        public int SaveCollection(EntityCollection<SanPhamEntity> currentCollectionSanPham)
        {
            int cntSave = 0;
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                cntSave = adapter.SaveEntityCollection(currentCollectionSanPham);
            }
            return cntSave;
        }

        public bool SaveEntity(SanPhamEntity currentEntitySanPham)
        {
            bool isSave = false;
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                isSave = adapter.SaveEntity(currentEntitySanPham);
            }
            return isSave;
        }

        public int DeleteCollection(EntityCollection<SanPhamEntity> currentCollectionSanPham)
        {
            int cntDelete = 0;
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.DeleteEntityCollection(currentCollectionSanPham);
            }
            return cntDelete;
        }

        public bool DeleteEntity(SanPhamEntity currentEntitySanPham)
        {
            bool isDelete = false;
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                isDelete = adapter.DeleteEntity(currentEntitySanPham);
            }
            return isDelete;
        }

        public System.Data.DataTable Report(ChungLoaiSanPhamEntity currentEntityChungLoaiSanPham)
        {
            System.Data.DataTable dtSanPham = new System.Data.DataTable();
            using (DataAccessAdapter adapter = new DataAccessAdapter())
            {
                dtSanPham = RetrievalProcedures.SanPhamByChungLoaiSanPhamPro(currentEntityChungLoaiSanPham.MaChungLoaiSanPham, adapter);
            }
            return dtSanPham;
        }
    }
}
